package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ServiceCategoryDTO;
import com.sky.dto.ServiceCategoryPageQueryDTO;
import com.sky.entity.ServiceCategory;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.ServiceCategoryMapper;
import com.sky.mapper.ServiceProviderMapper;
import com.sky.result.PageResult;
import com.sky.service.ServiceCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 服务分类业务层
 */
@Service
@Slf4j
public class ServiceCategoryServiceImpl implements ServiceCategoryService {

    @Autowired
    private ServiceCategoryMapper serviceCategoryMapper;
    @Autowired
    private ServiceProviderMapper serviceProviderMapper;

    /**
     * 新增服务分类
     * @param serviceCategoryDTO
     */
    public void save(ServiceCategoryDTO serviceCategoryDTO) {
        ServiceCategory serviceCategory = new ServiceCategory();
        //属性拷贝
        BeanUtils.copyProperties(serviceCategoryDTO, serviceCategory);

        //分类状态默认为禁用状态0
        serviceCategory.setStatus(StatusConstant.DISABLE);

        //设置创建时间、修改时间、创建人、修改人
//        serviceCategory.setCreateTime(LocalDateTime.now());
//        serviceCategory.setUpdateTime(LocalDateTime.now());
//        serviceCategory.setCreateUser(BaseContext.getCurrentId());
//        serviceCategory.setUpdateUser(BaseContext.getCurrentId());

        serviceCategoryMapper.insert(serviceCategory);
    }

    /**
     * 分页查询
     * @param serviceCategoryPageQueryDTO
     * @return
     */
    public PageResult pageQuery(ServiceCategoryPageQueryDTO serviceCategoryPageQueryDTO) {
        PageHelper.startPage(serviceCategoryPageQueryDTO.getPage(),serviceCategoryPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<ServiceCategory> page = serviceCategoryMapper.pageQuery(serviceCategoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除服务分类
     * @param id
     */
    public void deleteById(Long id) {
        //查询当前分类是否关联了服务人员，如果关联了就抛出业务异常
        Integer count = serviceProviderMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有服务人员，不能删除
            throw new DeletionNotAllowedException(MessageConstant.SERVICE_CATEGORY_BE_RELATED_BY_PROVIDER);
        }

        //查询当前分类是否关联了服务套餐，如果关联了就抛出业务异常
        // TODO: 当ServicePackageMapper创建后，添加此检查
        // count = servicePackageMapper.countByCategoryId(id);
        // if(count > 0){
        //     //当前分类下有服务套餐，不能删除
        //     throw new DeletionNotAllowedException(MessageConstant.SERVICE_CATEGORY_BE_RELATED_BY_PACKAGE);
        // }

        //删除分类数据
        serviceCategoryMapper.deleteById(id);
    }

    /**
     * 修改服务分类
     * @param serviceCategoryDTO
     */
    public void update(ServiceCategoryDTO serviceCategoryDTO) {
        ServiceCategory serviceCategory = new ServiceCategory();
        BeanUtils.copyProperties(serviceCategoryDTO,serviceCategory);

        //设置修改时间、修改人
//        serviceCategory.setUpdateTime(LocalDateTime.now());
//        serviceCategory.setUpdateUser(BaseContext.getCurrentId());

        serviceCategoryMapper.update(serviceCategory);
    }

    /**
     * 启用、禁用服务分类
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        ServiceCategory serviceCategory = ServiceCategory.builder()
                .id(id)
                .status(status)
//                .updateTime(LocalDateTime.now())
//                .updateUser(BaseContext.getCurrentId())
                .build();
        serviceCategoryMapper.update(serviceCategory);
    }

    /**
     * 根据类型查询服务分类
     * @param type
     * @return
     */
    public List<ServiceCategory> list(Integer type) {
        return serviceCategoryMapper.list(type);
    }
} 